package com.pickgliss.loader
{
	import com.pickgliss.ui.ComponentSetting;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.SoundManager;
	
	import flash.utils.ByteArray;
	
	/**
	 * 后台资源加载
	 */	
	public class BackResourceLoader
	{
		private static var _instance:BackResourceLoader;
		public static function get instance():BackResourceLoader
		{
			if(!_instance)_instance = new BackResourceLoader();
			return _instance;
		}
		private var _resourceList:Array ;
		private var _loader:BaseLoader;
		public function BackResourceLoader()
		{
			_resourceList = [];
		}
		
		public function addResource(path:String):void
		{
			if(_resourceList.indexOf(path) >= 0)return;
			_resourceList.push(path);
			loadNext();
		}
		
		private function loadNext():void
		{
			if(_loader)return;
			if(_resourceList.length > 0)
			{
				var path:String = _resourceList.pop();
				var md5:String =  ComponentSetting.getUrlMd5(path)
				var ba:ByteArray = LoaderSavingManager.loadCachedFile(path,false,md5);
				if(ba && ba.length > 0)
				{
					if(ComponentSetting.checkNeedAnalyzeMd5(md5))
					{
						if(ComponentSetting.compareMD5(ba,md5))
						{
							loadNext();
							return;
						}
					}
					else
					{
						loadNext();
						return;
					}
				}
				path = LoaderManager.Instance.getBackResourceLoadPath(path,BaseLoader.MODULE_LOADER);
				_loader = new BaseLoader(LoaderManager.Instance.getNextLoaderID(),path);
				_loader.addEventListener(LoaderEvent.COMPLETE,__completeHandler);
				_loader.addEventListener(LoaderEvent.PROGRESS, __loaderProgressHandler);
				_loader.loadFromWeb();
			}
		}
		
		private function __completeHandler(e:LoaderEvent):void
		{
			var url:String = _loader.url;
			var md5:String = ComponentSetting.getUrlMd5(url);
			LoaderSavingManager.cacheFile(url,_loader.content,false,md5);
			LoaderSavingManager.saveFilesToLocal();
			if(url.indexOf("audio")>=0)
			{
				var path:String = ComponentSetting.getProjectSourcePath("audio.swf");
				var loader:BaseLoader = LoaderManager.Instance.creatLoader(path,BaseLoader.MODULE_LOADER);
				loader.addEventListener(LoaderEvent.COMPLETE,__audioCompleteHandler);
				LoaderManager.Instance.startLoad(loader);
			}
			_loader.removeEventListener(LoaderEvent.COMPLETE,__completeHandler);
			_loader.removeEventListener(LoaderEvent.PROGRESS, __loaderProgressHandler);
			_loader.dispose();
			_loader = null;
			loadNext();
		}
		private function __loaderProgressHandler(evt : LoaderEvent):void
		{
//			if(evt.loader.progress > .2)removeLoader(evt.loader.url);
		}
		
		private function __audioCompleteHandler(e:LoaderEvent):void
		{
			e.loader.removeEventListener(LoaderEvent.COMPLETE,__audioCompleteHandler);
			SoundManager.instance.setupAudioResource();
		}
		public function removeLoader(path:String):void
		{
			if(!_loader)return;
			var key : String = _loader.url;
			if(key.indexOf("?") > 0)
			{
				key = key.substr(0,key.indexOf("?"));
			}
			if(path == key)
			{
				_loader.removeEventListener(LoaderEvent.COMPLETE,__completeHandler);
				_loader.removeEventListener(LoaderEvent.PROGRESS, __loaderProgressHandler);
				_loader.dispose();
				_loader = null;
				loadNext();
			}
		}
		
		public function removeResource(path:String):void
		{
			if(_resourceList.indexOf(path) >= 0)
				_resourceList.splice(_resourceList.indexOf(path),1);
		}
	}
}